package jnpf.database.sql.impl;

import jnpf.database.enums.DbAliasEnum;
import jnpf.database.enums.DbSttEnum;
import jnpf.database.source.DbBase;
import jnpf.database.sql.SqlBase;

/**
 * 类功能
 *
 * @author JNPF开发平台组
 * @version V3.2.0
 * @copyright 引迈信息技术有限公司
 * @date 2021/10/19
 */
public class SqlPostgre extends SqlBase {

    @Override
    protected void setSqlBase() {
        fieldListSql =  "SELECT\n" +
                "\tbase.\"column_name\"\n"+DbAliasEnum.FIELD_NAME.AS()+",\n" +
                "\tcol_description ( t1.oid, t2.attnum )\n"+DbAliasEnum.FIELD_COMMENT.AS()+",\n" +
                "\tbase.udt_name\n"+DbAliasEnum.DATA_TYPE.AS()+",\n" +
                "\tt2.attnotnull AS\t"+ DbAliasEnum.ALLOW_NULL.AS()+",\n" +
                "\tCOALESCE(character_maximum_length, numeric_precision, datetime_precision)\n"+DbAliasEnum.DATA_LENGTH.AS()+",\n" +
                "\t(CASE\n" +
                "\t\tWHEN ( SELECT t2.attnum = ANY ( conkey ) FROM pg_constraint WHERE conrelid = t1.oid AND contype = 'p' ) = 't' \n" +
                "\t\tTHEN 1 ELSE 0 \n" +
                "\tEND ) \n" + DbAliasEnum.PRIMARY_KEY.AS() +
                "\nFROM\n" +
                "\tinformation_schema.COLUMNS base,\n" +
                "\tpg_class t1,\n" +
                "\tpg_attribute t2 \n" +
                "WHERE\n" +
                "\tbase.\"table_name\" = '"+DbSttEnum.TABLE.getTarget()+"' \n" +
                "\tAND t1.relname = base.\"table_name\" \n" +
                "\tAND t2.attname = base.\"column_name\" \n" +
                "\tAND t1.oid = t2.attrelid \n" +
                "\tAND t2.attnum > 0;\n";
        tableListSql =
                "select relname as "+ DbAliasEnum.TABLE_NAME.AS() +",cast(obj_description(relfilenode,'pg_class') " +
                        "as varchar) as " +DbAliasEnum.TABLE_COMMENT.AS()+ ",0 " + DbAliasEnum.TABLE_SUM.AS()
                        + " from pg_class c\n" +
                        "where relname in (select tablename from pg_tables where schemaname='public'" +
                        " and position('_2' in tablename)=0);";
    }

    @Override
    public String[] getPageSql(String sql, String sortType, Integer currentPage, Integer pageSize) {
        return new String[0];
    }

    public SqlPostgre(DbBase dbBase){
        super(dbBase);
    }

}
